#include <string.h>
#include <stdlib.h>




char* 	strcpy(char* s, const char* ct)
{
	int i = 0;
	do { s[i] = ct[i]; } 
		while (ct[i++] != '\0');
	return s;
}

 
char*	strncpy(char* s, const char* ct, size_t n)
{
	int i;

	for ( i = 0; i < n; i++ )
	{
		s[i] = ct[i]; 
		if ( ct[i] == '\0' ) break;
	}
	
	while ( i < n )
		s[i++] = '\0'; 

	   
	return s;
}

 
char* 	strcat(char* s, const char* ct)
{
	char *tmp = s;
	while(*s) s++;
	while((*s++ = *ct++) != '\0');
	return tmp;
}

 
char* 	strncat(char* s, const char* ct, size_t n)
{
	char *tmp = s;

	if(n > 0) 
	{
		while(*s) s++;
		while((*s++ = *ct++)) 
		{
			if (--n == 0) 
			{
				*s = '\0';
				break;
			}
		}
	}

	return tmp;
}

 
int 	strcmp(const char* cs, const char* ct)
{
	char c;

	while(1) 
	{
		if((c = *cs - *ct++) != 0 || !*cs++)
			break;
	}

	return c;
}

 
int 	strncmp(const char* cs, const char* ct, size_t n)
{
	char c = 0;

	while(n > 0) 
	{
		if ((c = *cs - *ct++) != 0 || !*cs++)
			break;
		n--;
	}

	return c;

}


char*	strndup(const char *s, size_t n)
{
	char *result;
	result = (char*)malloc( n + 1 );
	memcpy( result, s, n );
	result[n] = 0;
	return result;
}

char*	strdup(const char *s)
{
/*	char *result;
	int len;
	len = strlen(s);
	result = (char*)malloc( len + 1 );
	memcpy( result, s, len + 1 );
	return result;*/
}


  
char* 	strchr(const char* cs, int c)
{
	int i;
	for ( i = 0; i < strlen(cs); i++ )
		if ( cs[i] == c ) return (char*)(cs + i);
	return NULL;
}

 
char* 	strrchr(const char* cs, int c)
{
	int i;
	for ( i = strlen(cs) - 1; i >= 0; i-- )
		if ( cs[i] == c ) return (char*)(cs + i);
	return NULL;
}

size_t 	strlen(const char* cs)
{
	size_t ans = 0;
	while ( cs[ans++] != '\0' ) {};
	return (ans-1);
}


void* 	memcpy(void* s, const void* ct, size_t n)
{
	char *dest,*src;
	int i;

	dest = (char*)s;
	src = (char*)ct;
	for ( i = 0; i < n; i++)
		dest[i] = src[i];
	
	return s;
}

 
void* 	memmove(void* s, const void* ct, size_t n)
{
	char *dest,*src;
	int i;

	dest = (char*)s;
	src = (char*)ct;
	for ( i = 0; i < n; i++)
		dest[i] = src[i];
	
	return s;
}

 
int 	memcmp(const void* cs, const void* ct, size_t n)
{
	char *s,*t;
	int i;

	s = (char*)cs;
	t = (char*)ct;
	for ( i = 0; i < n; i++)
	{
		if ( s[i] < t[i] ) return -1;
		if ( s[i] > t[i] ) return  1;
	}
	return 0;
}

 
void* 	memchr(const void* cs, int c, size_t n)
{
	int i;
	for ( i = 0; i < n; i++)
		if (  ((char*)cs)[i] == c ) return (void*)(cs + i);

	return NULL;
}

 
void* 	memset(void* s, int c, size_t n)
{
	int i;
	for ( i = 0; i < n ; i++)
		((char*)s)[i] = c;
	
	return s;
}

 

